package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {


    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public List<Role> findAllRole() {
        return roleDao.findAll();
    }

    @Override
    public void add(Permission permission, Integer[] roleIds) {
            //新增权限
            permissionDao.add(permission);
            Integer permissionId = permission.getId();//权限id

            //设置权限和角色多对多关系
        //设置多对多关系
        if(roleIds != null && roleIds.length > 0){
            for (Integer roleId : roleIds) {//角色id
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",permissionId);
                permissionDao.setPermissionAndRole(map);
            }
        }

    }
    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    public List<Integer> findRoleIdsByPermissionId(Integer permissionId) {
        return permissionDao.findRoleIdsByPermissionId(permissionId);
    }

    @Override
    public void edit(Permission permission, Integer[] roleIds) {
        //编辑权限
        permissionDao.update(permission);
        Integer permissionId = permission.getId();//权限id
        //删除权限和角色多对多关系
        permissionDao.delPermissionAndRoleByPermissionId(permissionId);
        //重新设置权限和角色多对多关系
        if(roleIds != null && roleIds.length > 0){
            for (Integer roleId : roleIds) {//角色id
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",permissionId);
                permissionDao.setPermissionAndRole(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {

        //删除权限和角色多对多关系
        permissionDao.delPermissionAndRoleByPermissionId(id);
        //删除权限
        permissionDao.delete( id);
    }
}
